import {getRequest} from "./api";
//创建菜单初始化方法
export const initMenu = (router, store) => {
    //判断是否有菜单数据，有就不加载
    if (store.state.routes.length > 0) {
        // 说明有菜单数据，直接返回
        return;

    } else {
        //获取下拉菜单的组件项
        getRequest("/system/config/menu")
            .then(data => {
                if (data) {
                    let fmtRoutes = formatRoutes(data);
                    //添加符合 格式的路由
                    router.addRoutes(fmtRoutes);
                    //同时写入仓库,方便v-for读取,commit是同步,dispatch是异步
                    store.commit('initRoutes', fmtRoutes);

                    // store.dispatch('connect');
                }
            })
    }

}
export const formatRoutes = (data) => {
    let fmRoutes = [];
    data.forEach(router => {
        //ES6新增解构赋值
        // 1 解构源： 等号右边是要解构的对象。
        // 2 解构目标：等号左边是要解构的对象。
        //批量定义，相当于let path=router.path
        let {
            path,
            component,
            name,
            meta,
            iconCls,
            children
        } = router;
        //判断是否是一级菜单的children
        if (children && children instanceof Array) {
            //递归
            children = formatRoutes(children);
        }
        let fmRouter = {
            path: path,
            name: name,
            iconCls: iconCls,
            meta:meta,
            children: children,
            //动态加载
            component(resolve) {
                if (component.startsWith("Home")) {
                    require(['../views/' + component + '.vue'], resolve);
                } else if (component.startsWith("Emp")) {
                    require(['../views/emp/' + component + '.vue'], resolve);
                } else if (component.startsWith("Per")) {
                    require(['../views/per/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sal")) {
                    require(['../views/sal/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sta")) {
                    require(['../views/sta/' + component + '.vue'], resolve);
                } else if (component.startsWith("Sys")) {
                    require(['../views/sys/' + component + '.vue'], resolve);
                }
            }
            //使用下面,菜单项没有路径
            // component: (component) => {
            //     if (component.startsWith("Home")) {
            //         require(['../views/' + component + '.vue'], resolve);
            //     } else if (component.startsWith("Emp")) {
            //         require(['../views/emp/' + component + '.vue'], resolve);
            //     } else if (component.startsWith("Per")) {
            //         require(['../views/per/' + component + '.vue'], resolve);
            //     } else if (component.startsWith("Sal")) {
            //         require(['../views/sal/' + component + '.vue'], resolve);
            //     } else if (component.startsWith("Sta")) {
            //         require(['../views/sta/' + component + '.vue'], resolve);
            //     } else if (component.startsWith("Sys")) {
            //         require(['../views/sys/' + component + '.vue'], resolve);
            //     }
            // },

        }
        //fmRouter放入数组fmRoutes中
        fmRoutes.push(fmRouter);
    })
    return fmRoutes;
}